﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CXMLStudioIDE.Common;
using System.Data;
using CXMLStudioIDE.DataAccessLayer;
using CXMLStudioIDE.Common.EntityProperties;
using System.Diagnostics;
using System.Windows.Forms;

namespace CXMLStudioIDE.BusinessLayer
{
    public class PropertiesBL
    {
        private List<Type> m_types;

        public PropertiesBL()
        {
            m_types = Common.Common.GetTypes();
        }
        public PropertySet GetPropertiesForEntity(DisplayContext context, string s_EntityName, int i_EntityTypeVersionsID)
        {
            PropertiesDAL pDal = new PropertiesDAL();
            DataTable dt = pDal.GetPropertiesByEntityVersion(Convert.ToInt32(context), i_EntityTypeVersionsID);

            if (dt.Rows.Count <= 0)
                return null;

            PropertySet pSet = new PropertySet(s_EntityName);
            pSet.DisplayContext = context;
            EntityProperty ep = null;
            //Debug.WriteLine("starting GetPRopertiesForEntity:" + DateTime.Now.ToString());
            //if there are rows then create properties and send propertyset
            foreach (DataRow row in dt.Rows)
            {
                string strCategory = row["PropertyCategoryName"].ToString();
                string strPropName = row["PropertyName"].ToString();
                string strPropValue = row["StringValue"].ToString();
                string strPropGroup = row["PropertyGroupName"].ToString();
                string strPropDesc = row["PropertyDescription"].ToString();
                //Type t = CXMLStudioIDE.Common.Common.GetTypeFromString(strCategory);
                Type t = GetType(strCategory);
                if (t != null)
                {
                    //Debug.WriteLine(t.Name);
                    object o = ControlFactory.GetValueObjectFromString(t, strPropValue, null);
                    ep = new EntityProperty(strPropName, o, t, false, true, strPropGroup, strPropDesc, null);
                    pSet.Add(ep);
                }
            }
            //Debug.WriteLine("Finished GetPRopertiesForEntity:" + DateTime.Now.ToString());
            return pSet;
        }

        public void SaveControlBinaryProperties(object ctrl, int i_EntityTypeVersionsID, string sZ_Lineage, DisplayContext context, ControlType ctrl_type)
        {

            if (i_EntityTypeVersionsID > 0 && sZ_Lineage != null && context != null)
            {
                PropertiesDAL pDal = new PropertiesDAL();
                byte[] bytes = ControlFactory.SerializeControlToBin(ctrl);
                if (bytes != null)
                {
                    pDal.SaveBinaryControl(i_EntityTypeVersionsID, sZ_Lineage, bytes, Convert.ToInt32(context), Convert.ToInt32(ctrl_type));
                }
            }
        }

        public object GetControlFromBinary(int i_EntityTypeVersionsID, string sZ_Lineage, DisplayContext context, ControlType ctrl_type)
        {
            object tmpcontrol = null;
            PropertiesDAL propsDal = new PropertiesDAL();
            byte[] bytes = propsDal.GetBinaryControl(i_EntityTypeVersionsID, sZ_Lineage, Convert.ToInt32(context),Convert.ToInt32(ctrl_type));
            if (bytes != null)
            {
                tmpcontrol = ControlFactory.DeserializeControlFromBin(bytes, null, null, null);
            }
            return tmpcontrol;
        }

        public Type GetType(string s_Type)
        {
            foreach (Type t in m_types)
            {
                if (t.ToString().Equals(s_Type))
                {
                    return t;
                }
            }
            return null;
        }
    }

}
